package com.tsite.common.report;

import java.io.ByteArrayOutputStream;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

class PdfReportEngine extends ReportEngineAbstract {

	@Override
	protected <T> boolean handleReport(ReportRequest<T> reportRequest,
			ByteArrayOutputStream resultStream) {
		
		boolean isHandled = false;
		if (reportRequest.getReportType().equals(ReportType.PDF)) {
			isHandled = true;
			try {
				
				// Create a JRXlsExporter instance
				JRPdfExporter exporter = new JRPdfExporter();
				
				JasperPrint jp = this.fillingReportTemplate(reportRequest);				
				 
				// Here we assign the parameters jp and baos to the exporter
				exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resultStream);	
				
				exporter.exportReport();
				
			} catch (JRException jre) {
				throw new RuntimeException(jre);
			}
		}
		return isHandled;
	}

}
